package edu.mit.media.fv.components
{
	import edu.mit.media.fv.blocks.Model;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.BevelFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.utils.Timer;

	public class AQBlockPopup extends MovieClip
	{
		public static const NORTH:String = "north";
		public static const SOUTH:String = "south";
		public static const EAST:String = "east";
		public static const WEST:String = "west";
		public static const AROUND:String = "around";
		
		private static var popup:AQBlockPopup;
		
		private var position:String = AROUND;
		private var margin:int = 10;
		private var outer:int = 100;
		private var inner:int = 35;
		private var model:Model = null;
		private var menuItems:Array = new Array();//array of display objects
		private var wall:Sprite = new Sprite();
		
		
		//fan out operations
		private var timer:Timer;
		private var shade:Sprite = new Sprite();
		private const partitions:int = 10;
		private const theta:Number = Math.PI*2/partitions;
		private const center:Number = outer*3;
			
		public function AQBlockPopup()
		{
			super();
			
			this.wall.graphics.beginFill(0xBFF59A);
			this.wall.graphics.drawCircle(0,0,100);
			this.wall.graphics.drawCircle(0,0,35);
			this.wall.graphics.endFill();
			this.wall.filters = [new BevelFilter(4,45,0xFFFFFF, 0.50,0x000000,1), new GlowFilter(0x000000,0.6,30,30)];
			this.addChild(this.wall);
			
			this.addChild(this.shade);
			this.mask = this.shade;
			this.timer = new Timer(25, partitions);
			this.timer.addEventListener(TimerEvent.TIMER, onTimer);
			this.timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
		}
		private function reformMenuItems(menuItems:Array):void{
			for (var j:int = 0; j <this.menuItems.length; j++){
				this.removeChild(this.menuItems[j]);
			}
			
			this.menuItems = menuItems;
			var phase:Number = 0;
			if(this.position == NORTH){
				phase = Math.PI;
			}else if(this.position == SOUTH){
			}else if(this.position == EAST){
			}else if(this.position == WEST){
			}else if(this.position == AROUND){
				
			}		
			
			var size:int = 4;
			var width:Number = ((outer-inner)/1.41 - margin);
			var theta:Number = Math.PI*2/size;
			var center:Number = 0.5*(outer+inner);
			for (var i:int = 0; i <this.menuItems.length; i++){
				var item:DisplayObject = this.menuItems[i];
				item.width = width;
				item.height = width;
				item.x = center*Math.cos(phase+theta*i)-width/2;
				item.y = center*Math.sin(phase+theta*i)-width/2;
				this.addChild(item);
			}
		}
		public static function show(x_raw:int, y_raw:int, position:String, o:DisplayObject, menuItems:Array):void{
			if(popup == null && o.stage != null){
				popup = new AQBlockPopup();
				o.stage.addEventListener(MouseEvent.MOUSE_DOWN, popup.fanIn);
			}
			if(o.stage != null){
				
				popup.position = position;
				o.stage.addChild(popup);
				var pp:Point = o.localToGlobal(new Point(x_raw, y_raw));
				popup.x = pp.x
				popup.y = pp.y;
				popup.reformMenuItems(menuItems);
				popup.fanOut();
			}
		}
		public function fanIn(event:Event):void{
			if (this.stage != null){
				if(event.target is DisplayObject){
					if(!this.contains(DisplayObject(event.target))){
						this.stage.removeChild(this);
					}
				}
			}
			this.shade.graphics.clear();
		}
		public function fanOut():void{
			this.shade.graphics.clear();
			tx = 0;
			ty = 0;
			this.timer.reset();
			this.timer.start();
		}
		
		var tx:int = 0;
		var ty:int = 0;
		public function onTimer(event:TimerEvent):void{
			//trace("SSS on timer"+timer.currentCount);
			
			this.shade.graphics.beginFill(0xFFFFFF);
			this.shade.graphics.moveTo(0,0);
			
			//tx = center*Math.cos(theta*(timer.currentCount-1));
			//ty = center*Math.sin(theta*(timer.currentCount-1));
			this.shade.graphics.lineTo(tx, ty);
			//trace(theta*timer.currentCount+" degrees, "+tx+", "+ty);
			
			tx = center*Math.cos(theta*timer.currentCount);
			ty= center*Math.sin(theta*timer.currentCount);
			this.shade.graphics.lineTo(tx, ty);
			//trace(theta*timer.currentCount+" degrees, "+tx+", "+ty);
			
			this.shade.graphics.lineTo(0,0);
			this.shade.graphics.endFill();
			//trace("EEE on timer"+timer.currentCount);
		}
		public function onTimerComplete(event:TimerEvent):void{
			trace("timer complete");
			this.shade.graphics.clear();
			this.shade.graphics.beginFill(0xFFFFFF);
			this.shade.graphics.drawCircle(0,0,this.outer*2);
			this.shade.graphics.endFill();
		}
		
		
	}
}